//
//  HYYNetworkTool.swift
//  Hyy9Weibo
//
//  Created by Air on 16/3/27.
//  Copyright © 2016年 hyy. All rights reserved.
//

import UIKit
import AFNetworking

class HYYNetworkTool: NSObject {

    //单例
    static let sharedInstance: HYYNetworkTool = HYYNetworkTool() //类里面是可以访问private方法
    
    //afn
    private let afnManager : AFHTTPSessionManager = AFHTTPSessionManager(baseURL: HYYBaseURL)
    
    //私有化构造函数
    private override init() {
        super.init()
        //设置afn支持text/plain类型
        afnManager.responseSerializer.acceptableContentTypes?.insert("text/plain")
    }
    
    //如果在控制器或VM，直接调用AFN的方法，如果AFN以后不好用，想换其他框架或AFN升级，请求参数更换，到时候更换非常麻烦
    ///自己来封装GET请求
    func requestGET(URLString: String,parameters: AnyObject?,requestCallback:(result:[String:AnyObject]?,error:NSError?)->()){
        afnManager.GET(URLString, parameters: parameters, progress: nil, success: { (_, result) in
            //网络请求回来都是json格式
            let dict = result as? [String:AnyObject]
            requestCallback(result: dict, error: nil)
            }) { (_, error) in
                requestCallback(result: nil, error: error)
        }
    }
    
    ///自己来封装POST请求
    func requsetPOST(URLString: String,parameters: AnyObject?,requsetCallback:(result:[String:AnyObject]?,error:NSError?)->()) {
        afnManager.POST(URLString, parameters: parameters, progress: nil, success: { (_, result) in
            //网络请求回来都是json格式
            let dict = result as? [String:AnyObject]
            requsetCallback(result: dict, error: nil)
            }) { (_, error) in
                requsetCallback(result: nil, error: error)
        }
    }
}
